package com.gxkj.movie.controllers;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxkj.movie.config.utils.OkHttpCli;
import com.gxkj.movie.controllers.seo.IndexSeo;
import com.gxkj.movie.helpers.MovSiteHelpper;
import com.gxkj.movie.helpers.VodJiexiRecommendHelpper;
import com.gxkj.movie.threads.BaiduSubmitUrlThread;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@Controller
@Slf4j
public class VipViewController  extends BaseController {


    protected  static JSONArray  vipDataList = new JSONArray();

    @Autowired
    private OkHttpCli okHttpCli;


    @Autowired
    private VodJiexiRecommendHelpper  vodJiexiRecommendHelpper;


    private static Map<String ,JSONArray> dataListMap = new HashMap<>();


    /**
     * https://www.919yy.com/
     * 原来参考网页：https://www.919yy.com/aq/#
     * @param req
     * @param model
     * @return
     */

    @GetMapping("/vip/videosee")
    public String videosee(HttpServletRequest req, Model model,@RequestParam(value = "forceclear" ,required=false,defaultValue ="false") Boolean forceclear){


        renderHeader(  req,   model);
        renderData(req,   model,  forceclear);
//
        return getTempletate(req)+"/video_vipsee";

    }

    @GetMapping("/vip/videosee_detail")
    public String index(HttpServletRequest req, Model model,String url){
        renderHeader(  req,   model);
        model.addAttribute("url",url);

        return getTempletate(req)+"/ifream";
    }

    @Override
    public void renderHeader(HttpServletRequest req, Model model){
        super.renderHeader(req,model);
        model.addAttribute("menu", MenuEnum.VIP_VIDEO.getKey());
        model.addAttribute("TITLE", IndexSeo.TITLE);
        model.addAttribute("KEYWORDS",IndexSeo.KEYWORDS);
        model.addAttribute("DESCRIPTION",IndexSeo.DESCRIPTION);
        model.addAttribute("SITE_NAME", IndexSeo.SITE_NAME);
    }

    public void  renderData(HttpServletRequest req, Model model,Boolean forceClear){


        vipDataList =  dataListMap.get("vodJiexiList");
        if(forceClear){
            vipDataList.clear();
        }
        if(vipDataList == null){
            vipDataList = new JSONArray();
        }
        if(  vipDataList.isEmpty()){

            synchronized (VipViewController.class){
                if(  vipDataList.isEmpty()){
                    String key = "";
                    int pageSize = 60;
                    JSONObject pager = vodJiexiRecommendHelpper.findVodeJiexiRecommendData(1,pageSize,key);
                    if(pager != null){
                        JSONArray list = pager.getJSONArray("list");
                        if(list != null ){
                            vipDataList.addAll(list);
                            int total = pager.getInteger("total");
                            int   countPage = total / pageSize;
                            if (total % pageSize != 0){
                                countPage ++;
                            }
                            for(int i=2;i<=countPage;i++){
                                pager = vodJiexiRecommendHelpper.findVodeJiexiRecommendData(i,pageSize,key);
                                if(pager != null){
                                    list = pager.getJSONArray("list");
                                    if(list != null ){
                                        vipDataList.addAll(list);
                                    }
                                }
                            }
                        }
                    }
                    dataListMap.put("vodJiexiList",vipDataList);
                }
            }



        }



        model.addAttribute("vodJiexiList",vipDataList);

    }


}
